package org.pc2r.game.model;

import static org.pc2r.protocole.RequestBuilder.MAX_X;
import static org.pc2r.protocole.RequestBuilder.MIN_X;

import java.util.ArrayList;
import java.util.List;

import org.pc2r.game.Game;

public class Player {

  private String name;
  private Position drone;
  private int action;
  private List<SubMarine> submarines;

  public Player(String name, List<SubMarine> submarines) {
    this.name = name;
    this.action = 1;
    this.submarines = submarines;
    generateDrone();
  }

  public List<SubMarine> getSubmarines() {
    return submarines;
  }

  public boolean isDead() {
    // Si un joueur n'a pas de sous-marins il est considéré comme mort
    boolean isDead = (submarines == null || submarines.isEmpty());
    if(isDead) return true;
    
    // S'il existe au moins un sous-marin en vie, le joueur n'est pas mort
    for(SubMarine sub : submarines) {
      if(sub.isDead() == false) {
        return false;
      }
    }
    return true;
  }

  public void flagSubMarines() {
    for (SubMarine sub : submarines)
      sub.setPlayer(this);
  }

  public void deleteDeadSubmarines() {
    for (int i = 0; i < submarines.size(); i++) {
      if (submarines.get(i).isDead()) {
        submarines.remove(i);
        i--;
      }
    }
  }

  public void setAction(int action) {
    this.action = action;
  }

  public int getAction() {
    return this.action;
  }

  public String getName() {
    return name;
  }

  public Position getDrone() {
    return drone;
  }

  public void move(char position) {
    switch (position) {
      case 'U':
        move(drone.getX(), drone.getY() - 1);break;
      case 'D':
        move(drone.getX(), drone.getY() + 1);break;
      case 'L':
        move(drone.getX() - 1, drone.getY() );break;
      case 'R':
        move(drone.getX() + 1, drone.getY());break;
    }
  }

  public List<SubMarine> fire(Game game) {
    List<SubMarine> touched = new ArrayList<>(4);
    for (SubMarine sub : game.getSubmarines()) {
      if (sub.touch(drone.getX(), drone.getY())) {
        touched.add(sub);
      }
    }
    return touched;
  }

  public void move(int x, int y) {
    drone.setX(x);
    drone.setY(y);
    if (drone.getX() < MIN_X) {
      drone.setX(MIN_X);
    }
    if (drone.getX() > MAX_X) {
      drone.setX(MAX_X);
    }
    if (drone.getY() < MIN_X) {
      drone.setY(MIN_X);
    }
    if (drone.getY() > MAX_X) {
      drone.setY(MAX_X);
    }
  }

  private void generateDrone() {
    int x = (int) (16 * Math.random());
    int y = (int) (16 * Math.random());
    drone = new Position(x, y);
  }

  @Override
  public String toString() {
    return name;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Player other = (Player) obj;
    if (this.name == null) {
      if (other.name != null)
        return false;
    } else if (!this.name.equals(other.name))
      return false;
    return true;
  }
}
